<template>
  <div class='setting-container'>
    <el-card>
      <!-- 导航 -->
      <div slot="header">
        <my-breadcrumb>个人设置</my-breadcrumb>
      </div>

      <!-- 栅格

      el-row: 可以分成24份-->
      <el-row>
        <el-col :span="12">
          <!-- 表单 -->
          <el-form label-width="120px">
            <el-form-item label="编号：">100</el-form-item>
            <el-form-item label="手机：">{{userInfo.mobile}}</el-form-item>
            <el-form-item label="媒体名称：">
              <el-input v-model="userInfo.name"></el-input>
            </el-form-item>
            <el-form-item label="媒体简介：">
              <el-input v-model="userInfo.intro" type="textarea" :rows="3"></el-input>
            </el-form-item>
            <el-form-item label="联系邮箱：">
              <el-input v-model="userInfo.email"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="hSave" type="primary" :loading="isloading">保存设置</el-button>
            </el-form-item>
          </el-form>
        </el-col>

        <el-col :span="12">
          <!-- 上传组件 action必须属性-->
          <!--
            在修改用户头像这个功能中，后端接口是Patch方式，而el-upload默认只支持post
            方式上传，

            所以这里我们要启用el-upload的自定义上传属性:http-request ，它会覆盖默认的上传方式

          -->
          <el-upload
            class="avatar-uploader"
            action=""
            :http-request="uploadPhoto"
            :show-file-list="false">
            <img v-if="userInfo.photo" :src="userInfo.photo" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <p class="edit-photo">修改头像</p>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>
<script>
export default {
  data () {
    return {
      userInfo: {
        id: '',
        email: '',
        intro: '',
        mobile: '',
        name: '',
        photo: ''
      },
      isloading: false
    }
  },
  created () {
    this.loadUserInfo()
  },
  methods: {
    // 发送请求获取数据
    async loadUserInfo () {
      try {
        const res = await this.$http({
          url: '/mp/v1_0/user/profile',
          method: 'GET'
        })
        // 将真实数据回填
        this.userInfo.id = res.data.data.id
        this.userInfo.email = res.data.data.email
        this.userInfo.intro = res.data.data.intro
        this.userInfo.mobile = res.data.data.mobile
        this.userInfo.name = res.data.data.name
        this.userInfo.photo = res.data.data.photo
      } catch (err) {
        console.log(err)
      }
    },
    async hSave () {
      const { name, intro, email } = this.userInfo
      try {
        this.isloading = true // 加载loading
        const res = await this.$http({
          url: '/mp/v1_0/user/profile',
          method: 'PATCH',
          data: { // 参数
            name,
            intro,
            email
          }
        })
        console.log(res)
        this.$message.success('修改成功')
        this.$eventBus.$emit('person', { name, intro, email }) // 发布
        this.isloading = false
      } catch (err) {
        console.log(err)
        this.$message.error('修改失败')
        this.isloading = false
      }
    },
    async uploadPhoto (obj) { // 上传头像
      const { file } = obj
      const fd = new FormData()
      // 添加参数
      fd.append('photo', file)
      try {
        const res = await this.$http({
          url: '/mp/v1_0/user/photo',
          method: 'PATCH',
          data: fd
        })
        console.log(res)
        this.$message.success('更新头像成功')
        this.userInfo.photo = res.data.data.photo
        this.$eventBus.$emit('person', { photo: res.data.data.photo })
      } catch (err) {
        console.log(err)
        this.$message.error('更新头像失败')
      }
    }
  }

}
</script>
<style lang="less" scoped>
  .avatar{
    width: 178px;
    height: 178px;
    line-height: 178px;
  }
  .avatar-uploader {
      text-align: center;
  }
  .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
  }
  .avatar-uploader /deep/ .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
  }
.edit-photo{
  font-size: 12px;
  text-align: center;
}
</style>
